package com.ygou.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygou.pojo.Admininfo;
import com.ygou.pojo.Slide;
import com.ygou.service.impl.SlideServiceImpl;
import com.ygou.utils.AppendContentToFile;
import com.ygou.utils.FileUDUtil;

@Controller
@RequestMapping(value="/slide")
public class SlideController {
	@Autowired
	SlideServiceImpl sService;
	@RequestMapping("/show")
	public String showAll(ModelMap model){
		List<Slide> slides=sService.showAll();
		model.put("slides", slides);
		return "admin/advertising";
	}
	@RequestMapping(value="/openUpdate")
	public String openUnpdate(int id,ModelMap model){
		Slide upds=sService.selById(id);
		model.put("upds", upds);
		return "admin/slide_upd";
	}
	@RequestMapping(value="/doUpd")
	public String doUpd(int id,int goodid,String title,ModelMap model,HttpSession session){
		Slide s=sService.selById(id);
		s.setId(id);
		s.setGoodId(goodid);
		s.setGoodName(title);
		String fname=FileUDUtil.FILENAME.toString();
		if(fname.length()>2){
			int index1=fname.indexOf("[");
			int index2=fname.indexOf("]");
			fname=fname.substring(index1+1, index2);
			s.setPicName("/filecontent/slide/"+fname);
		}
		sService.upd(s);
		session.setAttribute("slides",sService.showAll());
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int adminid=100;
		if(admin!=null){
			adminid=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(adminid+"", "修改了幻灯片");
		return "redirect:show";
	}
	@RequestMapping(value="/doAdd",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean doAdd(int goodid,String title,ModelMap model,HttpSession session){
		Slide s=new Slide();
		s.setGoodId(goodid);
		s.setGoodName(title);
		String fname=FileUDUtil.FILENAME.toString();
		int index1=fname.indexOf("[");
		int index2=fname.indexOf("]");
		fname=fname.substring(index1+1, index2);
		s.setPicName("/filecontent/slide/"+fname);
		sService.add(s);
		session.setAttribute("slides",sService.showAll());
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "添加了幻灯片");
		return true;
	}
	@RequestMapping("/del")
	public String dodel(int id,HttpSession session){
		sService.del(id);
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int adminid=100;
		if(admin!=null){
			adminid=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(adminid+"", "删除了幻灯片");
		session.setAttribute("slides",sService.showAll());
		return "redirect:show";
	}
	@RequestMapping("/dels")
	public String dodels(String[] delChe,HttpSession session){
		if(delChe==null){
			
		}else{
			for (String s1 : delChe) {
				sService.del(Integer.parseInt(s1));
			}
		}
		Admininfo admin= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(admin!=null){
			id=admin.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "删除了幻灯片");
		session.setAttribute("slides",sService.showAll());
		return "redirect:show";
	}
	
	
	/*@RequestMapping(value="/showSlide",produces="application/json;charset=UTF-8")
	public @ResponseBody List<Slide> showSlide(){
		List<Slide> slides=sService.showAll();
		return slides;
	}*/
	
	@RequestMapping(value="/showSlide")
	public String showSlide(HttpServletRequest session){
		List<Slide> slides=sService.showAll();
		session.setAttribute("slides",slides);
		return "index";
	}
	
}
